package com.tuananh.radnom.model.userinfo;

import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.tuananh.radnom.model.netservice.MyMapTypeAdapter;
import com.tuananh.radnom.model.notification.Receiver;

public class User {
	String name;
	HashMap<String, String> params = new HashMap<String, String>();

	@XmlJavaTypeAdapter(MyMapTypeAdapter.class)
	public HashMap<String, String> getParams() {
		return params;
	}

	public String getName() {
		return name;
	}

	@XmlAttribute
	public void setName(String name) {
		this.name = name;
	}

	public void setParams(HashMap<String, String> params) {
		this.params = params;
	}

	public static User getFromName(ArrayList<User> users, String name) {
		if (users != null && name != null)
			for (User usr : users) {
				if (usr.getName().equals(name))
					return usr;
			}
		return null;
	}

	public static ArrayList<User> getUsersFromReceivers(
			ArrayList<Receiver> receivers, ArrayList<User> users,
			ArrayList<UserGroup> groups) {

		HashMap<String, User> hashmap = new HashMap<String, User>();

		for (Receiver receiver : receivers) {
			if (receiver.getType().equals("group")) {
				UserGroup group = UserGroup.getFromName(groups,
						receiver.getName());
				if (group != null)
					for (User usr : group.getUsers()) {
						hashmap.put(usr.getName(), usr);
					}
			} else if (receiver.getType().equals("user")) {
				User usr = User.getFromName(users, receiver.getName());
				if (usr != null)
					hashmap.put(usr.getName(), usr);

			}
		}

		return new ArrayList<User>(hashmap.values());
	}
}
